import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;


public class mainUI extends JApplet {
	private static JTextField LongField;
	private static JTextField LatField;
	private static JTextField DateField;
	private JLabel lblDatemmddyyyy;
	private JLabel lblBrowseLocation;
	private JButton TodayBtn;
	//private static double longitude, latitude;
	private JButton gprint = new JButton("Print Gnomon");
	private JButton sprint = new JButton("Print Sundial");
	private Sundial s = new Sundial();
	private Gnomon g = new Gnomon();
	private JLabel lblWIs;
	private JLabel lblSIs;
	private static JCheckBox daylightCheck; //use the static method "daylight"

	/**
	 * Get the longitude
	 * @return double the longitude
	 */
	public static double getLongitude() {
		try {
			return Double.parseDouble(LongField.getText());
		} catch (Exception e) {
			System.out.println ("get long error");
			System.out.println(e.getMessage());
		} finally {
			return Double.parseDouble(LongField.getText());
		}
	}
	/**
	 * Get the latitude
	 * @return double the latitude
	 */
	public static double getLatitude() {
		try {
			return Double.parseDouble(LatField.getText());
		} catch (Exception e) {
			System.out.println ("get lat error");
			System.out.println(e.getMessage());
		} finally {
			return Double.parseDouble(LatField.getText());
		}
	}
	/**
	 * Check if daylight savings
	 * @return boolean
	 */
	public static boolean daylight() {
		return daylightCheck.isSelected();
	}
	/**
	 * Already used to check if date is valid right before calculation is called
	 * @return boolean
	 */
	public static boolean validDate() {
		SimpleDateFormat date = new SimpleDateFormat("ddmmyyyy");
		date.setLenient(false);
		try {
			@SuppressWarnings("unused")
			Date ddate = date.parse(DateField.getText());
			return true;
		} catch (ParseException e) {
			System.out.println("valid date error");
			System.out.println(e.getMessage());
			return false;
		}
	}
	
	/**
	 * gets the date from the date field or return an empty string if invalid
	 * @return String
	 */
	public static String getDate() {
		return (validDate())?DateField.getText():"";
	}
	
	public mainUI() {
		getContentPane().setBackground(Color.LIGHT_GRAY);
		getContentPane().setLayout(null);
		this.setSize(500, 600);
		
		JLabel lblBuildasundial = new JLabel("Build-A-Sundial");
		lblBuildasundial.setBounds(0, 0, 450, 49);
		lblBuildasundial.setFont(new Font("Helvetica Neue", Font.BOLD, 40));
		lblBuildasundial.setHorizontalAlignment(SwingConstants.CENTER);
		getContentPane().add(lblBuildasundial);
		
		LongField = new JTextField();
		LongField.setBounds(150, 83, 134, 28);
		getContentPane().add(LongField);
		LongField.setColumns(10);
		
		LatField = new JTextField();
		LatField.setBounds(150, 120, 134, 28);
		getContentPane().add(LatField);
		LatField.setColumns(10);
		
		JLabel lblLongitude = new JLabel("Longitude:");
		lblLongitude.setBounds(61, 89, 77, 16);
		getContentPane().add(lblLongitude);
		
		JLabel lblLatitude = new JLabel("Latitude:");
		lblLatitude.setBounds(61, 126, 61, 16);
		getContentPane().add(lblLatitude);
		
		DateField = new JTextField();
		DateField.setBounds(150, 160, 134, 28);
		getContentPane().add(DateField);
		DateField.setColumns(10);
		String theDate = new SimpleDateFormat("ddMMyyyy").format(Calendar.getInstance().getTime());
		DateField.setText(theDate);
		
		lblDatemmddyyyy = new JLabel("Date (DDMMYYYY):");
		lblDatemmddyyyy.setBounds(17, 166, 132, 16);
		getContentPane().add(lblDatemmddyyyy);
		
		lblBrowseLocation = new JLabel("Browse Location:");
		lblBrowseLocation.setBounds(17, 208, 121, 16);
		getContentPane().add(lblBrowseLocation);
		
		JComboBox location = new JComboBox();
		location.setModel(new DefaultComboBoxModel(new String[] {"Custom", "", "Los Angeles", "Chicago", "New York City", "Honolulu"}));
		location.setBounds(150, 204, 155, 27);
		location.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent src) {
				JComboBox box = (JComboBox) src.getSource();
				String locName = (String) box.getSelectedItem();
				
				if (locName.equals("Los Angeles")) {
					LatField.setText("34.0522");
					LongField.setText("-118.2428");
				} else if (locName.equals("Chicago")) {
					LatField.setText("41.8500");
					LongField.setText("-87.6500");
				} else if (locName.equals("New York City")) {
					LatField.setText("40.7142");
					LongField.setText("-74.0064");
				} else if (locName.equals("Honolulu")) {
					LatField.setText("21.3069");
					LongField.setText("-157.8583");
				} else {
					LatField.setText("");
					LongField.setText("");
				}
			}
		});
		getContentPane().add(location);
		
		JButton makeBtn = new JButton("Make!");
		makeBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					Calculator.latitude = Double.parseDouble(LatField.getText());
				} catch (Exception e) {
					System.out.println("latitude error");
					System.out.println(e.getMessage());
				}
				try {
					Calculator.longitude = Double.parseDouble(LongField.getText());
				} catch (Exception e) {
					System.out.println("longitude error");
					System.out.println("Error " + e.getMessage());
				}
				
				/***********************
				 *Call calculator 	   *		
				 **********************/
				if (validDate()) {
					JFrame f = new JFrame("Gnomon");
					try {
						LatField.setText(LatField.getText().trim());
						LongField.setText(LongField.getText().trim());
						if (!LatField.getText().equals("") || !LongField.getText().equals("")){
							double num = Calculator.calculate();
							g.setLatitude(Double.parseDouble(LatField.getText()));
							Double.parseDouble(LongField.getText());
							f.add(g);
							f.setSize(400, 400);
							f.setVisible(true);
							Calculator.time = DateField.getText();
							JFrame sf = new JFrame("Sundial");
							sf.add(s);
							sf.setSize(400, 400);
							sf.setVisible(true);
							s.setAngle(num);
							JFrame fp = new JFrame("Printer");
							fp.setLayout(new FlowLayout());
							fp.setVisible(true);
							fp.add(gprint);
							fp.add(sprint);
							fp.setSize(100, 100);
						}
					} catch (Exception e) {
						System.out.println("Calculator error");
						System.out.println(e.getMessage());
					}
				}
			}
		});
		makeBtn.setBounds(150, 252, 134, 29);
		getContentPane().add(makeBtn);

		gprint.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				PrinterJob job = PrinterJob.getPrinterJob();
				job.setPrintable(g);
				try{
					//if (job.printDialog()) {
						job.print();
					//}
				} catch (PrinterException p) {
					System.out.println("printer1 error");
					System.out.println(p.getMessage());
				}
			}
		});
		sprint.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				PrinterJob job = PrinterJob.getPrinterJob();
				job.setPrintable(s);
				try{
					//if (job.printDialog()) {
						job.print();
					//}
				} catch (PrinterException p) {
					System.out.println("printer1 error");
					System.out.println(p.getMessage());
				}
			}
		});
		
		TodayBtn = new JButton("Today");
		TodayBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String theDate = new SimpleDateFormat("ddMMyyyy").format(Calendar.getInstance().getTime());
				DateField.setText(theDate);
			}
		});
		TodayBtn.setBounds(296, 161, 117, 29);
		getContentPane().add(TodayBtn);
		
		lblWIs = new JLabel("* W is denoted by negative (-)");
		lblWIs.setBounds(296, 89, 198, 16);
		getContentPane().add(lblWIs);
		
		lblSIs = new JLabel("* S is denoted by negative (-)");
		lblSIs.setBounds(296, 126, 198, 16);
		getContentPane().add(lblSIs);
		
		daylightCheck = new JCheckBox("Check if observes daylight savings time");
		daylightCheck.setBounds(145, 61, 305, 23);
		getContentPane().add(daylightCheck);
		
	}
}
